<%@ Language=VBScript%>
<% 
	Session("FormName")="F38"	
%>
<!--#include file="..\CheckSession.inc"-->
<!--#include file=..\getFormName.inc-->
<html>
<head>
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>New Page 2</title>
</head>
<!--#include file="..\jvGrid.inc"-->
<!--#include file=..\vbFunction.inc-->
<!--#include file="..\pageSelected.inc"-->
<!--#include file=..\funTab.inc-->
<%	
Set ClsFun=server.CreateObject("Vinalines.ClsFunction")
Dim bLink 
mode=Request("mode")
bComm=Request("bComm")
bMa=Request("bMa")
bMacu=Request("bMacu")
bMaDonViOld=Request("bMaDonViOld")	

if bComm="detail" then		
'	bMa=Request("bMa")
'	bMacu=Request("bMacu")
'	bMaDonViOld=Request("bMaDonViOld")	
	sSQL="Select * from DonVi where ID Like '"& bMa &"'"
	Set RecDL=server.CreateObject("ADODB.Recordset")
	RecDL.CursorType=3
	RecDL.CacheSize=5
	RecDL.Open sSQL,Conn
	if RecDL.RecordCount then	
		bTen=RecDL("TenDonVi")
		bTencu=RecDL("TenDonVi")
		bKyhieu=RecDL("Kyhieu")
		Loaitim=Request("Loaitim")
		bsearch=Request("bsearch")
		bMaDonVi=RecDL("MaDonVi")
	end if
	RecDL.Close
	set RecDL=nothing
else
	bTen=Request("bTen")
	bTencu=Request("bTencu")
	bKyhieu=Request("bKyhieu")
	Loaitim=request("Loaitim")
	bsearch=request("bsearch")
	bMaDonVi=Request("bMaDonVi")
end if

ChuyenDonVi=request("ChuyenDonVi")
if ChuyenDonVi<>"" then
	Session("bMaDonVi")=ChuyenDonVi
	Msg="Bạn đã chuyển đơn vị làm việc thành đơn vị : " & bTen
end if

Dim bTempComm
Dim bMsg
Dim Msg	
	if Session("ClsFun").LayGiaTriQuyen(Session("Permision"),Session("FormName"),2)="1" or Session("IsAdmin") then	
	Select Case bComm
		Case "add"
			if not KTMa(bMa,bComm) then
				if not KTTen(bTen,bComm) then							
					if  bMaDonVi <>"" then
						sSQL="Insert into DonVi(ID,tendonvi,madonvi,kyhieu) values('"& ClsFun.ModiString(bMa) &"','"& ClsFun.ModiString(bTen) &"','"& TimCon(bMaDonVi) &"','"& ClsFun.ModiString(bKyhieu) &"')"
					else
						sSQL="Insert into DonVi(ID,tendonvi,kyhieu)values('"& ClsFun.ModiString(bMa) &"','"& ClsFun.ModiString(bTen) &"','"& ClsFun.ModiString(bKyhieu) &"')"
					end if
					Conn.Execute sSQL
				else
					Msg="T&#234;n &#273;&#227; t&#7891;n t&#7841;i"
					bMsg="ten"
				end if
			else
				Msg="Mã &#273;&#227; t&#7891;n t&#7841;i"
				bMsg="ma"
			end if
		case "delete"
			set RecKT=server.CreateObject("ADODB.Recordset")
			RecKT.CursorType=3
			RecKT.CacheSize=5
			ssql="Select macanbo from canbo where madonvi like (select madonvi from DonVi where ID='"& ClsFun.ModiString(bma) &"')"		
			RecKT.Open ssql,Conn
			if RecKT.RecordCount<=0 then
				if Not ChonNutGoc(bma) then
					if not KTraCon(bma) then
						sSQL="Delete from DonVi Where ID Like '"& ClsFun.ModiString(bMa) &"'"
						Conn.Execute sSQL
					else
						Msg=" đang chứa nhóm con"
						bMsg="madonvi"
					end if
				else
					Msg="Không xoá được nút gốc"
				end if
			else
				Msg="Đơn vị &#273;ang ch&#7913;a c&#225;n b&#7897; kh&#244;ng th&#7875; xo&#225; &#273;&#432;&#7907;c"
			end if
			RecKT.Close
			set RecKT=nothing
		case "edit"
			if not KTMa(bMa,"edit") then
				'Neu ma thay doi
				if lcase(bmacu)<>lcase(bma) then 
					if not KTTen(bTen,"edit") then
						sUpdateMaDonVi=""
						if Not ChonNutGoc(bMacu) then
							if bMaDonVi <>"" then
								if lcase(bMaDonViOld)<>lcase(bMaDonVi) then 
									if KTraCon(bMacu) then
										Msg=" đang chứa nhóm con"
										bMsg="madonvi"
									else
										if ChonTrungMa(bMacu,bMaDonVi) then
											Msg=" không được nằm trong chính nó"
											bMsg="madonvi"						
										else
											sUpdateMaDonVi=",madonvi='"& TimCon(bMaDonVi) &"' "
										end if
									end if	
								else
									if not CoMaDonVi(bMacu) then
										sUpdateMaDonVi=",madonvi='"& TimCon(bMaDonVi) &"' "
									end if
								end if 
							else 'Neu ma don vi trong
								if KTraCon(bMacu) then
									Msg=" đang chứa nhóm con"
									bMsg="madonvi"
								else
									sUpdateMaDonVi=",madonvi=''"
								end if
							end if	'bMaDonVi <>""
						end if	'ChonNutGoc

						if CoVTDen(bMa) then
								Msg=" đang chứa văn thư cần xử lý"
								bMsg="madonvi"
						end if
						
						if bMsg="" then
							sSQL="Update DonVi set ID='"& ClsFun.ModiString(bma) _
									&"',tendonvi='"& ClsFun.ModiString(bTen) _	
									&"',kyhieu='" & ClsFun.ModiString(bKyhieu)_
									& sUpdateMaDonVi &" Where ID Like '" & ClsFun.ModiString(bMacu) &"'"
							Conn.Execute sSQL
						end if
					else	'KTTen
						Msg="T&#234;n &#273;&#227; T&#7891;n t&#7841;i"
						bMsg="ten"
					end if	'KTTen
				else 'ma khong thay doi
					if not KTTen(bTen,"edit") then								
							sUpdateMaDonVi=""
							if Not ChonNutGoc(bMacu) then
								if bMaDonVi <>"" then
									if lcase(bMaDonViOld)<>lcase(bMaDonVi) then 
										if KTraCon(bMacu) then
											Msg=" đang chứa nhóm con"
											bMsg="madonvi"
										else
											if ChonTrungMa(bMacu,bMaDonVi) then
												Msg=" không được nằm trong chính nó"
												bMsg="madonvi"					
											else
												sUpdateMaDonVi=",madonvi='"& TimCon(bMaDonVi) &"' "
											end if
										end if	
									else
										if not CoMaDonVi(bMacu) then
											sUpdateMaDonVi=",madonvi='"& TimCon(bMaDonVi) &"' "
										end if
									end if 
								else 'Neu ma don vi trong
									if KTraCon(bMacu) then
										Msg=" đang chứa nhóm con"
										bMsg="madonvi"
									else
										sUpdateMaDonVi=",madonvi=''"
									end if
								end if
							end if

							if CoVTDen(bMa) then
									Msg=" đang chứa văn thư cần xử lý"
									bMsg="madonvi"
							end if								

							if bMsg="" then
								sSQL="Update DonVi set tendonvi='"& ClsFun.ModiString(bTen) _
								&"',kyhieu='" & ClsFun.ModiString(bKyhieu)_
								&"'" & sUpdateMaDonVi &" Where ID Like '" & ClsFun.ModiString(bMacu) & "'"									
								Conn.Execute sSQL
							end if
					else
						Msg="T&#234;n &#273;&#227; T&#7891;n t&#7841;i"
						bMsg="ten"
					end if
				end if
			else 
				Msg="M&#227; &#273;&#227; T&#7891;n t&#7841;i"
				bMsg="ma"
			end if
	End Select
End if
Set Rec=server.CreateObject("ADODB.Recordset") 
rec.CursorType = 3
rec.CacheSize=5		
select case Request("bComm")
	case "search"	
		if IsRoot(Session("bMaDonVi")) or Session("ClsFun").LayGiaTriQuyen(Session("Permision"),Session("FormName"),3)="1" or Session("IsAdmin") then
				strWhereNhom= ""
		else
			strWhereNhom= " and madonvi like '"& LEFT(Session("bMaDonVi"),4) &"%' "
		end if			
		ssql="Select * from DonVi where tendonvi like '%"& ClsFun.ModiString(bsearch) & "%' " & strWhereNhom & " order by madonvi"
		
	case else			
		Loaitim=""
		bsearch=""
		if IsRoot(Session("bMaDonVi")) or Session("ClsFun").LayGiaTriQuyen(Session("Permision"),Session("FormName"),3)="1" or Session("IsAdmin") then
			strWhereNhom= ""
		else
			strWhereNhom= " where madonvi like '"& LEFT(Session("bMaDonVi"),4) &"%' "
		end if
		ssql="Select * from DonVi " & strWhereNhom & " order by madonvi"
		'response.write bMaDonVi
end select
Dim nRecCount	' Number of records found
Dim nPageCount	' Number of pages of records we have
Dim nPage	' Current page number
nPage=CLng(request("Page"))	
if nPage="" then nPage=1
rec.Open ssql,Conn
if  rec.EOF and rec.BOF then
	nPageCount=0
end if	
%>
<%if Msg<>"" then%>
  <% if bMsg="ma" then %>
			<center><font size=3 color=red face='Times New Roman'>M&#227; đơn vị: <%=bma%> &#273;&#227; t&#7891;n t&#7841;i</font></center>
   <%end if%>
  <% if bMsg="ten" then %>
			<center><font size=3 color=red face='Times New Roman'>T&#234;n: <%=bten%> &#273;&#227; t&#7891;n t&#7841;i</font></center>
   <%end if%>
   <% if bMsg="malienket" then %>
			<center><font size=3 color=red face='Times New Roman'><%=Msg%></font></center>
   <%end if%>
   <% if bMsg="madonvi" then %>
			<center><font size=3 color=red face='Times New Roman'>Đơn vị: <%=bten%><%=Msg%> </font></center>
   <%end if%>
   <% if bMsg="" then %>
			<center><font size=3 color=red face='Times New Roman'><%=Msg%></font></center>
   <%end if%>
<%end if%>
<!--#include file="..\jvfunction.inc"-->
<script language=VBScript runat=server>
	'Kiem tra ma trung
	Function KTMa(bValue,loai)
		KTMa=false
		if loai="add" then
			ssql="Select * from DonVi where ID like '"& trim(ClsFun.ModiString(bValue)) &"'"
			Set RecTemp=server.CreateObject("ADODB.Recordset") 
			RecTemp.Open ssql,Application("StrConn")
			if (not rectemp.BOF) and (not RecTemp.EOF) then
				KTMa=True
			end if
			set RecTemp=nothing
		end if
		if loai="edit" then
			if Lcase(bMacu)<>Lcase(bValue) then
				ssql="Select * from DonVi where ID like '"& trim(ClsFun.ModiString(bValue)) &"'"
				Set RecTemp=server.CreateObject("ADODB.Recordset") 
				RecTemp.Open ssql,Application("StrConn")
				if (not rectemp.BOF) and (not RecTemp.EOF) then
					KTMa=True
				end if
				set RecTemp=nothing
			end if		
		end if
	End function
	'Kiem tra ten trung
	Function KTTen(bValue,loai)
		KTTen=false
		if loai="add" then
			ssql="Select Tendonvi from DonVi where Tendonvi like '"& trim(ClsFun.ModiString(bValue)) &"'"
			Set RecTemp=server.CreateObject("ADODB.Recordset") 
			RecTemp.Open ssql,Application("StrConn")
			if (not rectemp.BOF) and (not RecTemp.EOF) then
				KTTen=True
			end if
			set RecTemp=nothing
		end if
		if loai="edit" then
			if Lcase(bTencu)<>Lcase(bValue) then
				ssql="Select Tendonvi from DonVi where tendonvi like '"& trim(ClsFun.ModiString(bValue)) &"'"
				Set RecTemp=server.CreateObject("ADODB.Recordset") 
				RecTemp.Open ssql,Application("StrConn")
				if (not rectemp.BOF) and (not RecTemp.EOF) then
					KTTen=True
				end if
				set RecTemp=nothing
			end if		
		end if
	End function
	'Kiem tra truoc khi Xoa ban ghi co quan he voi bang khac khong
	Function KTQuanhe(bValue,TableName)
		KTQuanhe=false
	End Function

	Function TimCha(Item)	'Ham tim xem cha cua 1 node dua vao =?
		if Item <>"000000" then
			if Mid(Item,5,2)="00" and Mid(Item,3,2)<>"00" then
				TimCha="000000"
			else
				TimCha="00" & Mid(Item,3,2) & "00"
			end if		
		else
			TimCha=""
		end if 
	End Function

	Function TimCon(Item)	'Ham tim xem con tiep theo cua 1 node dua vao =?	 Chu y neu Item=""
		if Item ="000000" then 'Cap 1 
			ssql="select max(madonvi) as madonvi from DonVi where SUBSTRING(madonvi,5,2) like '00'"
			Set RecTemp=server.CreateObject("ADODB.Recordset") 
			RecTemp.Open ssql,Application("StrConn")
			if (not RecTemp.BOF) and (not RecTemp.EOF) then
				bNum=cint(mid(RecTemp("madonvi"),3,2))+1
				if bNum<100 then
					TimCon="00" &  right("0" & cstr(bNum),2) & "00"
				else	'Neu den 99 so - Kiem tra lai tu dau xem cho nao rong thi chen vao. 
					TimCon=SinhMaCap1
				end if
			end if
			RecTemp.Close
			set RecTemp=nothing
		else 
			if  cint(mid(Item,3,2))>=1 then	'Cap 2
				ssql="select max(madonvi) as madonvi  from DonVi where SUBSTRING(madonvi,3,2) like '" & mid(Item,3,2) & "'"
				Set RecTemp=server.CreateObject("ADODB.Recordset") 
				RecTemp.Open ssql,Application("StrConn")
				if (not RecTemp.BOF) and (not RecTemp.EOF) then				
					bNum=cint(mid(RecTemp("madonvi"),5,2))+1
					if bNum<100 then
						TimCon=left(Item,4) &  right("0" & cstr(bNum),2) 
					else	'Neu den 99 so - Kiem tra lai tu dau xem cho nao rong  thi chen vao. 
						TimCon=SinhMaCap2(Item)
					end if
				end if
				RecTemp.Close
				set RecTemp=nothing			
			else
				TimCon=""
			end if
		end if 
	End Function

	Function SinhMaCap1()		'Ham sinh ma don vi cap 1
		strTemp=""
		SinhMaCap1=""
		for i = 1 to 99
			strTemp="00" &  right("0" & cstr(i),2) & "00"
			if not KTMaDonVi(strTemp) then
				SinhMaCap1=strTemp
				exit for
			end if
		next		
	End Function

	Function SinhMaCap2(Item)		'Ham sinh ma don vi cap 2
		strTemp=""
		SinhMaCap2=""
		for i = 1 to 99
			strTemp=left(Item,4) &  right("0" & cstr(i),2) 
			if not KTMaDonVi(strTemp) then
				SinhMaCap2=strTemp
				exit for
			end if
		next		
	End Function

	Function KTMaDonVi(bValue)	'Ha`m kie^?m tra xem ma~ ddo+n vi. dda~ to^`n ta.i hay chu+a
		KTMaDonVi=false
		ssql="select madonvi from DonVi where madonvi='"& bValue &"'"
			Set RecTemp=server.CreateObject("ADODB.Recordset") 
			RecTemp.Open ssql,Application("StrConn")
			if (not RecTemp.BOF) and (not RecTemp.EOF) then
				KTMaDonVi=true
			end if
			RecTemp.Close
			set RecTemp=nothing
	End Function
	
	Function KTraCon(bID)	'Ham kiem tra xem node da co con chua ?		
		KTraCon=false
		ssql="select count(madonvi) as numMaDonvi from DonVi " & _
					"where SUBSTRING(madonvi,3,2) in " & _
					"(select SUBSTRING(madonvi,3,2) from DonVi where ID='"& bID &"' and SUBSTRING(madonvi,5,2)='00')"
			Set RecTemp=server.CreateObject("ADODB.Recordset") 
			RecTemp.Open ssql,Application("StrConn")
			if (not RecTemp.BOF) and (not RecTemp.EOF) then
				if cint(RecTemp("numMaDonvi"))>1 then
					KTraCon=true
				end if
			end if
			RecTemp.Close
			set RecTemp=nothing
	End Function

	Function ChonTrungMa(bID,bMaDonvi)	'Ham kiem tra xem viec chon ma co hop le khong
		ChonTrungMa=false
		ssql="select madonvi from DonVi where ID='"& bID &"'"
			Set RecTemp=server.CreateObject("ADODB.Recordset") 
			RecTemp.Open ssql,Application("StrConn")
			if (not RecTemp.BOF) and (not RecTemp.EOF) then
				if RecTemp("madonvi")=bMaDonvi then
					ChonTrungMa=true
				end if
			end if
			RecTemp.Close
			set RecTemp=nothing
	End Function

	Function ChonNutGoc(bID)	'Ham kiem tra xem co phai NSD sua nut goc khong
		ChonNutGoc=false
		ssql="select madonvi from DonVi where ID='"& bID &"'"
			Set RecTemp=server.CreateObject("ADODB.Recordset") 
			RecTemp.Open ssql,Application("StrConn")
			if (not RecTemp.BOF) and (not RecTemp.EOF) then
				if RecTemp("madonvi")="000000" then
					ChonNutGoc=true
				end if
			end if
			RecTemp.Close
			set RecTemp=nothing
	End Function

	Function CoMaDonVi(bID)	'Ha`m kie^?m tra xem nu't ddo' dda~ co' ma~ ddo+n vi. chu+a ?
		CoMaDonVi=false
		ssql="select madonvi from DonVi where ID='"& bID &"'"
			Set RecTemp=server.CreateObject("ADODB.Recordset") 
			RecTemp.Open ssql,Application("StrConn")
			if (not RecTemp.BOF) and (not RecTemp.EOF) then
				if not isnull(RecTemp("madonvi")) and RecTemp("madonvi")<>""  then 
					CoMaDonVi=true
				end if
			end if
			RecTemp.Close
			set RecTemp=nothing
	End Function

	Function CoVTDen(bMa)		'Ha`m kie^?m tra xem ddo+n vi. ddo' co' o+? VTDen chu+a ?
		CoVTDen=false
		if bMa<>"" then
			ssql="select ID from vtden where donvixuly like + '%'+ (select madonvi from DonVi where ID='"& bMa &"')+ '%'"
				Set RecTemp=server.CreateObject("ADODB.Recordset") 
				RecTemp.Open ssql,Application("StrConn")
				if (not RecTemp.BOF) and (not RecTemp.EOF) then
						CoVTDen=true
				end if
				RecTemp.Close
				set RecTemp=nothing
		end if
	End Function

</script>

<script language=VBScript  runat="server">
Function Paging(ByVal intPage, ByVal intPageCount, ByVal intRecordCount)
	Dim strQueryString
	Dim strScript
	Dim intStart
	Dim intEnd
	Dim strRet
	Dim i

	If intPage > intPageCount Then
		intPage = intPageCount
	ElseIf intPage < 1 Then 
		intPage = 1
	End If
	
	If intRecordCount = 0 Then
		'strRet = "No Records Found"
	ElseIf intPageCount = 1 Then
		'strRet = "End Of Hits"
	Else
		For i = 1 To Request.QueryString.Count
			If LCase(Request.QueryString.Key(i)) <> "page" Then
				strQueryString = strQueryString & "&" 
				strQueryString = strQueryString & Server.URLEncode(Request.QueryString.Key(i)) & "=" 
				strQueryString = strQueryString & Server.URLEncode(Request.QueryString.Item(i))
			End If
		Next

		For i = 1 To Request.Form.Count
			If LCase(Request.Form.Key(i)) <> "page" Then
				strQueryString = strQueryString & "&" 
				strQueryString = strQueryString & Server.URLEncode(Request.Form.Key(i)) & "=" 
				strQueryString = strQueryString & Server.URLEncode(Request.Form.Item(i))
			End If
		Next

		If Len(strQueryString) <> 0 Then
			strQueryString = "?" & Mid(strQueryString, 2) & "&"
		Else
			strQueryString = "?"
		End If

		strScript = Request.ServerVariables("SCRIPT_NAME") & strQueryString
	
		If intPage <= 10 Then
			intStart = 1
		Else
			If (intPage Mod 10) = 0 Then
				intStart = intPage - 9
			Else
				intStart = intPage - (intPage Mod 10) + 1
			End If
		End If

		intEnd = intStart + 9
		If intEnd > intPageCount Then intEnd = intPageCount
	
		'strRet = "Page " & intPage & " of " & intPageCount & ": "
	
		If intPage > 10 Then 
			strRet = strRet & "<a href=""" & strScript
			strRet = strRet & "page=" & intPage - 10 
			strRet = strRet & """><IMG border=0 SRC=../images/prev.gif></a> "
		End If
	
		For i = intStart To intEnd
			If i = intPage Then
				strRet = strRet & "<b><font color=A90A08>" & i & "</font></b> "
			Else
				strRet = strRet & "<a href=""" & strScript
				strRet = strRet & "page=" & i 
				strRet = strRet & """>" & i & "</a>"
				If i <> intEnd Then strRet = strRet & " "
			End If
		Next
	
		If intPage <> intPageCount and intPageCount > 10 Then
			strRet = strRet & " <a href=""" & strScript
			strRet = strRet & "page=" & intPage + 10 
			strRet = strRet & """><IMG border=0 SRC=../images/next.gif></a> "
		End If
	End If
	
	Paging = strRet
End Function
</script>

<SCRIPT ID=clientEventHandlersJS LANGUAGE=javascript>
<!--
function ChangePage(){
	strURL= nhom.page.value;
	window.nhom.target="right";
	window.nhom.action=strURL;
	window.nhom.submit();
}

function KTNhap(){
	if (nhom.TxtMa.value==""){
		alert("Bạn cần phải nhập mã");
		nhom.TxtMa.focus();
		return false;
	}
	if(daucach(nhom.TxtMa.value)){
		alert("Mã không cho phép chứa ký tự đặc biệt (',space)");
		nhom.TxtMa.focus();
		return false;
	}
	if (nhom.TxtTen.value==""){
		alert("Bạn cần phải nhập tên");
		nhom.TxtTen.focus();
		return false;
	}
	return true;
}

function CmdAdd_onclick() {
	nhom.TxtMa.value="<%=TaoMa("ID","donvi")%>"
	nhom.TxtTen.value="";
	nhom.TxtKyhieu.value="";
	cmdenable(false);
	Unlockdata();
	nhom.sua.value="2";
	nhom.TxtTen.focus();	
}
function cmdenable(bvalue){
	if (bvalue){
		if ("<%= Session("ClsFun").LayGiaTriQuyen(Session("Permision"),Session("FormName"),2)%>"=='1'||"<%= Session("IsAdmin") %>".toLowerCase()=='true'){
			nhom.CmdAdd.disabled= false;
		}			
		else{
			nhom.CmdAdd.disabled= true;		
		}	
		nhom.CmdDelete.disabled= false;
		nhom.CmdEdit.disabled= false;
		nhom.CmdSearch.disabled=false;
		
		nhom.CmdCancel.disabled= true;
		nhom.CmdOK.disabled= true;

		nhom.CmdDsCanbo.disabled= false;
		nhom.CmdChuyenDonvi.disabled= false;
	}
	else {
		nhom.CmdAdd.disabled= true;
		nhom.CmdDelete.disabled= true;
		nhom.CmdEdit.disabled= true;
		nhom.CmdSearch.disabled=true;
		
		nhom.CmdCancel.disabled= false;
		nhom.CmdOK.disabled= false;

		nhom.CmdDsCanbo.disabled= true;
		nhom.CmdChuyenDonvi.disabled= true;	
	}	
	nhom.CmdSearch.value="Tì<u>m</u> kiếm";
}

function CmdCancel_onclick() {
	cmdenable(true);
	nhom.CmdDelete.disabled= true;
	nhom.CmdEdit.disabled= true;
	Lockdata();
	nhom.TxtTen.value="";
	nhom.TxtMa.value=""
}

function Lockdata(){
	nhom.TxtMa.disabled=true;
	nhom.TxtTen.disabled=true;
	nhom.TxtKyhieu.disabled=true;
	nhom.comboDonVi.disabled=true;
	nhom.CmdDsCanbo.disabled=true;
	nhom.CmdChuyenDonvi.disabled=true;
}

function Unlockdata(){
	nhom.TxtMa.disabled=false;
	nhom.TxtTen.disabled=false;
	nhom.TxtKyhieu.disabled=false;
	nhom.comboDonVi.disabled=false;
}
function window_onload() {
//	CmdCancel_onclick();	
}	

function CmdExit_onclick() {
//	window.navigate("<%= Session("LastForm")%>");
}
function CmdDelete_onclick() {
	if (nhom.TxtMa.value==""){
		alert("Bạn hãy chọn thông tin cần xóa");
		return ;
	}
	if(!confirm("Bạn có muốn xóa không ?")){
		return false;	
	}
	ma=nhom.TxtMa.value;
	ten=nhom.TxtTen.value;
	cp=nhom.cPage.value
	b="frmDonVi.asp?Page="+cp+"&bma="+ma+"&bTen="+ten+"&bComm=delete"					
	window.nhom.target="right"
	window.nhom.action=b;
	window.nhom.submit();
	//window.navigate(b);
}
function CmdEdit_onclick() {
	if (nhom.TxtMa.value==""){
		alert("Bạn hãy chọn thông tin cần sửa");
		return ;
	}
	cmdenable(false);
	Unlockdata();
	<%bTempComm="edit"%>
	nhom.sua.value="1";
	nhom.Macu.value=nhom.TxtMa.value;
	nhom.Tencu.value=nhom.TxtTen.value
	nhom.MaDonViOld.value=nhom.comboDonVi.value	
	//alert('<%=cstr(bMaDonVi) %>');
	if ('<%=cstr(bMaDonVi) %>'=='000000'){
		nhom.comboDonVi.disabled=true;
	}
}
function CmdOK_onclick() {
	if (KTNhap()){
		cmdenable(true);
		//Lockdata();
		ma=nhom.TxtMa.value;
		ten=nhom.TxtTen.value;
		kyhieu=nhom.TxtKyhieu.value;
		macu=nhom.Macu.value;
		tencu=nhom.Tencu.value;
		bc=nhom.sua.value;
		cp=nhom.cPage.value;
		madonvi=nhom.comboDonVi.value;
		MaDonViOld=nhom.MaDonViOld.value;

		//alert (donvi);
		
		if (bc=="1"){
			b="frmdonvi.asp?Page="+cp+"&bMacu="+macu+"&bTencu="+tencu+"&bma="+ma+"&bTen="+ten+"&bKyhieu="+kyhieu+"&bMaDonVi="+madonvi+"&bMaDonViOld="+MaDonViOld+"&bComm=edit"
		}
		if(bc=="2") {
			b="frmdonvi.asp?Page="+cp+"&bma="+ma+"&bTen="+ten+"&bKyhieu="+kyhieu+"&bMaDonVi="+madonvi+"&bComm=add"				
		}
		window.nhom.target="right"
		window.nhom.action=b;
		window.nhom.submit();
	}
}
function CmdLoc_onclick() {
	Unlockdata();
	nhom.TxtMa.focus();
}


function CmdDsCanbo_onclick() {
	if(nhom.TxtMa.value==""){
		alert("Bạn cần phải chọn nhóm");
		return false;
	}	
	window.nhom.target="_blank";	
	nhom.action="frmDscanbo.asp?bMaDonVi=<%=bMaDonVi%>&bTenDonvi=<%=server.URLEncode(bTen)%>";
	nhom.submit();
	window.nhom.target="right";
}

function CmdSearch_onclick() {
	nhom.CmdAdd.disabled=true;
	nhom.CmdCancel.disabled=false;
	nhom.CmdEdit.disabled=true;
	nhom.CmdDelete.disabled=true;
	nhom.CmdDsCanbo.disabled=true;
	if (nhom.TxtTen.disabled){
		nhom.CmdSearch.value="Tì<u>m</u>";
		Unlockdata();
		nhom.TxtMa.value="";
		nhom.TxtTen.value="";
		nhom.TxtKyhieu.value="";
		Lockdata();
		nhom.TxtTen.disabled=false;
		nhom.TxtTen.focus();
		return false;
	}
	value=nhom.TxtTen.value;
	blink="frmdonvi.asp?bsearch="+value+"&bComm=search";
	window.nhom.action=blink;
	window.nhom.target="right";
	window.nhom.submit();
}

function CmdPrint_onclick() {
	window.nhom.target="new";
	window.nhom.action="../in/frmin.asp?bIn=nhom";
	window.nhom.submit();
}

function SetFocus(){
	document.nhom.CmdSearch.focus();
}

function CmdChuyenDonvi_onclick(){	
	//alert('<%=bTen%>');	
	ten=nhom.TxtTen.value;
	
	if ('<%=Session("bMaDonVi")%>'!='<%=bMaDonVi%>') {	
		window.nhom.action="frmdonvi.asp?bTen="+ten+"&ChuyenDonVi="+'<%=bMaDonVi%>';
		window.nhom.target="right";
		window.nhom.submit();	
	}
	else{
		alert("Bạn hãy chọn đơn vị cần chuyển khác đơn vị hiện tại");
	}

}
//-->
</SCRIPT>
<LINK href="../styles/styles.css" type=text/css rel=STYLESHEET>
<body  bgcolor=#F8F9D9 topmargin="0" leftmargin="0" style="font-family:times new roman" onload="SetFocus()">
<form name="nhom" method="post">
	<input type="hidden" name="sua" value="">
	<input type="hidden" name="Macu" value="">
	<input type="hidden" name="Tencu" value="">
	<input type="hidden" name="MaDonViOld" value="">
	<input type="hidden" name="cPage" value="<%=nPage%>">
	<input type="hidden" name="strURL" value="">
<p align=center><b><font FACE="Times New Roman" size="3">CẬP NHẬT ĐƠN VỊ</font></b>
<table border="0" cellpadding="0" cellspacing="0" style="Font-family:arial;font-size:13pt; BORDER-BOTTOM-STYLE: none; BORDER-LEFT-STYLE: solid; BORDER-RIGHT-STYLE: solid; BORDER-TOP-STYLE: solid;border-collapse:collapse; border-left-color:#C0C0C0; border-right-color:#C0C0C0; border-top-color:#C0C0C0; border-bottom-color:#C0C0C0" width="100%" bgcolor="#BDD5D0" bordercolor="#111111">
  <tr>
	<% if Session("ClsFun").LayGiaTriQuyen(Session("Permision"),Session("FormName"),2)="1" or Session("IsAdmin") then	%>
			<td width="12%">
				<Button name="CmdAdd" style="FONT-FAMILY: Arial;WIDTH: 100%" LANGUAGE=javascript onclick="return CmdAdd_onclick()" accesskey="T"><U>T</U>hêm</Button></td>
	    <%if  bcomm<>"detail" then%>
			<td width="12%">
				<Button disabled  name="CmdDelete" style="FONT-FAMILY: Arial; WIDTH: 100%" LANGUAGE=javascript onclick="return CmdDelete_onclick()" accesskey="X"><U>X</U>óa</Button></td>
		    <td width="12%">
		    		<Button disabled  name="CmdEdit" style="FONT-FAMILY: Arial;WIDTH: 100%" LANGUAGE=javascript onclick="return CmdEdit_onclick()" accesskey="S"><U>S</U>ửa</button></td>
		<%else%>
			<td width="12%">
				<Button name="CmdDelete" style="FONT-FAMILY: Arial; WIDTH: 100%" LANGUAGE=javascript onclick="return CmdDelete_onclick()" accesskey="X"><U>X</U>óa</Button></td>
		    <td width="12%">
		    		<Button name="CmdEdit" style="FONT-FAMILY: Arial;WIDTH: 100%" LANGUAGE=javascript onclick="return CmdEdit_onclick()" accesskey="S"><U>S</U>ửa</Button></td>
		<%end if%>
			<td width="12%">
				<Button name="CmdOK" disabled=true style="FONT-FAMILY: Arial;WIDTH: 100%" LANGUAGE=javascript onclick="return CmdOK_onclick()" accesskey="G"><P><U>G</U>hi nhận</P></Button></td>
		    <td width="12%">
		    		<Button name="CmdCancel" disabled=true style="FONT-FAMILY: Arial;WIDTH: 100%" LANGUAGE=javascript onclick="return CmdCancel_onclick()" accesskey="B"><P><U>B</U>ỏ qua</P></Button></td>
		    <td width="12%">
		    		<Button name="CmdSearch" style="FONT-FAMILY: Arial;WIDTH: 100%" LANGUAGE=javascript onclick="return CmdSearch_onclick()" accesskey="M"><P>Tì<u>m</u> kiếm</P> </Button></td>

	    <%if  bcomm<>"detail" then%>
	    		<td width="12%">
    			<Button disabled name="CmdDsCanbo" Style="FONT-FAMILY:Arial;WIDTH: 100%" LANGUAGE=javascript onclick="return CmdDsCanbo_onclick()" accesskey="D"><P><U>D</U>S Cán bộ</P></Button></td>
		<%else%>
			<td width="12%">
			<Button  name="CmdDsCanbo" style="FONT-FAMILY:Arial;WIDTH: 100%" LANGUAGE=javascript onclick="return CmdDsCanbo_onclick()" accesskey="D"><P><U>D</U>S Cán bộ</P></Button></td>
		<%end if%>

	    <%if  bcomm<>"detail" then%>
		    <td width="12%">
	    		<Button disabled name="CmdChuyenDonvi" style="FONT-FAMILY: Arial;WIDTH: 100%" LANGUAGE=javascript onclick="return CmdChuyenDonvi_onclick()" accesskey="C"><P><u>C</u>huyển Đơn vị</P> </Button></td>
		<%else%>
		    <td width="12%">
	    		<Button name="CmdChuyenDonvi" style="FONT-FAMILY: Arial;WIDTH: 100%" LANGUAGE=javascript onclick="return CmdChuyenDonvi_onclick()" accesskey="C"><P><u>C</u>huyển Đơn vị</P> </Button></td>
		<%end if%>
	<%else%>
		<td width="12%"><Button disabled name="CmdAdd" style="FONT-FAMILY: Arial;WIDTH: 100%" LANGUAGE=javascript onclick="return CmdAdd_onclick()" accesskey="T"><U>T</U>hêm</Button></td>
		<td width="12%"><Button disabled name="CmdDelete" style="FONT-FAMILY: Arial; WIDTH: 	100%" LANGUAGE=javascript onclick="return CmdDelete_onclick()" accesskey="X"><U>X</U>óa</Button></td>
		<td width="12%"><Button disabled name="CmdEdit" style="FONT-FAMILY: Arial;WIDTH: 100%" LANGUAGE=javascript onclick="return CmdEdit_onclick()" accesskey="S"><U>S</U>ửa</Button></td>
		<td width="12%"><Button name="CmdOK" disabled=true style="FONT-FAMILY: Arial;WIDTH: 100%" LANGUAGE=javascript onclick="return CmdOK_onclick()" accesskey="G"><P><U>G</U>hi nhận</P></Button></td>
		<td width="12%"><Button name="CmdCancel" disabled=true style="FONT-FAMILY: Arial;WIDTH: 100%" LANGUAGE=javascript onclick="return CmdCancel_onclick()" accesskey="B"><P><U>B</U>ỏ qua</P></Button></td>
		<td width="12%"><Button name="CmdSearch" style="FONT-FAMILY: Arial;WIDTH: 100%" LANGUAGE=javascript onclick="return CmdSearch_onclick()" accesskey="M"><P>Tì<u>m</u> kiếm</P> </Button></td>
		<td width="12%"><Button  disabled name="CmdDsCanbo" style="FONT-FAMILY:Arial;WIDTH: 100%" LANGUAGE=javascript onclick="return CmdDsCanbo_onclick()" accesskey="D"><P><U>D</U>S Cán bộ</P></Button></td>
 		<td width="12%"><Button disabled name="CmdChuyenDonvi" style="FONT-FAMILY: Arial;WIDTH: 100%" LANGUAGE=javascript onclick="return CmdChuyenDonvi_onclick()" accesskey="C"><P><u>C</u>huyển Đơn vị</P> </Button></td>

	<%end if%>
  </tr>
</table>
<table border="0" cellspacing="0" style="Font-family:Times New Roman;font-size:12pt;BORDER-BOTTOM-STYLE: solid; BORDER-LEFT-STYLE: solid; BORDER-RIGHT-STYLE: solid; BORDER-TOP-STYLE: none;border-collapse:collapse; border-left-color:#C0C0C0; border-right-color:#C0C0C0; border-top-color:#C0C0C0; border-bottom-color:#C0C0C0" width="100%" bordercolor="#111111" bgcolor="#BDD5D0">
<% if bcomm="detail" then %>
  <tr>
    <td width="12%">&nbsp;Mã</td>
    <td width="18%"><input name="TxtMa" disabled value="<%=bma%>" maxLength=10 style="FONT-FAMILY: Arial;width:100%" size="20" ></td> 
    <td width="15%">&nbsp;Tên đơn vị</td>
    <td width="55%"><input name="TxtTen" disabled value="<%=bten%>" maxLength=50 style="FONT-FAMILY: Arial;width:100%" size="20"></td>
  </tr>
  <tr>
  	<td width="12%">&nbsp;Ký hiệu</td>
  	<td width="18%"><input name="TxtKyhieu" disabled value="<%=bKyhieu%>" maxLength=10 style="FONT-FAMILY: Arial;width:100%" size="20" ></td>
  	<td width="15%">&nbsp;Thuộc đơn vị</td>
	<td width="55%">
		<select size="1" name="comboDonVi" disabled=true style="font-family: Arial;width:100%">
			<%		
               	Set RecNhom=server.CreateObject("ADODB.Recordset")               	
                if IsRoot(Session("bMaDonVi")) or Session("ClsFun").LayGiaTriQuyen(Session("Permision"),Session("FormName"),3)="1" or Session("IsAdmin") then
	               	strWhereNhom= " where SUBSTRING(madonvi,5,2) like '00' "
				else
					strWhereNhom= " where madonvi like '"& Session("bMaDonVi") &"' "
				end if
				ssql= "select * from DonVi " & strWhereNhom & " order by madonvi"
					
				RecNhom.Open ssql,Conn
				if (not RecNhom.EOF) or (not RecNhom.BOF) then
					RecNhom.MoveFirst
						do while not RecNhom.EOF%>		                			
						<% bTab=funTab(RecNhom("madonvi"))%>
						<% if RecNhom("madonvi")=TimCha(bMaDonVi) then%>
							<option selected value="<%=RecNhom("madonvi")%>"><% =bTab%><%=RecNhom("tendonvi")%>
						<% else %>                			
								<option value="<%=RecNhom("madonvi")%>"><% =bTab%><%=RecNhom("tendonvi")%>
							<% end if %>
							
							<%RecNhom.MoveNext
						loop
				end if
				RecNhom.Close
				set RecNhom=nothing %>
       	</select></td>  	
	  </tr>  
  <%else%>  	
	<tr>
    <td width="12%">&nbsp;Mã</td>
    <td width="18%"><input name="TxtMa" disabled maxLength=10 style="FONT-FAMILY: Arial;width:100%" size="20"></td>
    <td width="15%">&nbsp;Tên đơn vị</td>
    <td width="55%"><input name="TxtTen" disabled maxLength=50 style="FONT-FAMILY: Arial;width:100%" size="20"></td>
  </tr>
  <tr>
	  
    <td width="12%">&nbsp;Ký hiệu</td>
	  <td width="18%"><input name="TxtKyhieu" disabled maxLength=10 style="FONT-FAMILY: Arial;width:100%" size="20"></td>
	  <td width="15%">&nbsp;Thuộc đơn vị</td>
	  <td width="55%">
		<select size="1" name="comboDonVi" disabled=true style="font-family: Arial;width:100%">
			<%					
				set RecNhom=server.CreateObject("ADODB.Recordset")
				if IsRoot(Session("bMaDonVi")) or Session("ClsFun").LayGiaTriQuyen(Session("Permision"),Session("FormName"),3)="1" or Session("IsAdmin") then
					strWhereNhom= " where SUBSTRING(madonvi,5,2) like '00' "
				else
					strWhereNhom= " where madonvi like '"& Session("bMaDonVi") &"' "
				end if
				ssql= "select * from DonVi " & strWhereNhom & " order by madonvi"
                	
                	RecNhom.Open ssql,Conn
					if (not RecNhom.EOF) or (not RecNhom.BOF) then
                		RecNhom.MoveFirst
                			do while not RecNhom.EOF%>
							<% bTab=funTab(RecNhom("madonvi"))%>                			
                				<option value="<%=RecNhom("madonvi")%>"><% =bTab%><%=RecNhom("tendonvi")%>                				
                				<%RecNhom.MoveNext
                			loop
                	end if
				RecNhom.Close
				set RecNhom=nothing %>
			</select></td>	  
	  </tr>
<%end if%>
</table>
<!-- Danh sach don vi -->
<table border="0" style="Font-family:Times new roman;font-size:12pt; BORDER-BOTTOM-STYLE: solid; BORDER-LEFT-STYLE: solid; BORDER-RIGHT-STYLE: solid; BORDER-TOP-STYLE: none;Border-color:#C0C0C0" WIDTH="100%">
  <tr>
    <td width="100%" colspan="3" bgcolor="#173584" align="center"><b><font color="#FFFFFF" size="2">DANH SÁCH ĐƠN VỊ</font></b></td>
  </tr>
  <tr bgcolor="#BDD5D0" align="center">
    <td width="10%"><b>Mã</b></td>
    <td width="15%"><b>Ký hiệu</b></td>
    <td width="75%"><b>Tên đơn vị</b></td>
  </tr>
  <%	
	If Not Rec.Eof Then
		nRecCount = Rec.RecordCount
		Rec.PageSize = 20
		nPageCount = Rec.PageCount
		If nPage < 1 then
			nPage = 1			
		End If
		if nPage > nPageCount Then
			nPage=nPageCount
		end if
		cnt=(pg-1)*pgsize + 1		    
		STT=1
		rec.AbsolutePage=nPage
		howmanyrecs=0
		do while not Rec.EOF and howmanyrecs<rec.PageSize
			if (cnt/2=int(cnt/2)) then
				color="#eeeecc"
			else
				color="#F8F9D9"
			end if
			if Rec("Madonvi")="000000" then 
				fontweight=700
			else
				fontweight=100
			end if
%>  
  <tr id=mnu_<%=STT%> style="BORDER-RIGHT: #e7e7e7 1px solid; BORDER-TOP: #e7e7e7 1px solid; BORDER-LEFT: #e7e7e7 1px solid; COLOR: #666666; BORDER-BOTTOM: #e7e7e7 1px solid; HEIGHT: 21px; BACKGROUND-COLOR: <%=color%>" onmouseover="javascript:MouseOverMenu('mnu_<%=STT%>')" onmouseout="javascript:MouseOutMenu('mnu_<%=STT%>','<%=color%>')">
  	<% bTab=funTab(Rec("madonvi"))%>
	<% if Session("bMaDonVi")=Rec("madonvi") then %>
		<td width="10%" style="FONT-STYLE: italic" align="center"><A onmouseout="window.status='';return true" onmouseover="window.status='Ch&#7885;n th&#244;ng tin';return true" style='text-decoration: none;color:blue;font-weight:<%=fontweight%>' href="frmDonVi.asp?page=<%=nPage%>&bma=<%=Rec("ID")%>&bComm=<%="detail"%>">&nbsp;<%=Rec("ID")%></A></td>
		<td width="15%" style="FONT-STYLE: italic"><A onmouseout="window.status='';return true" onmouseover="window.status='Ch&#7885;n th&#244;ng tin';return true" style='text-decoration: none;color:blue;font-weight:<%=fontweight%>' href="frmDonVi.asp?page=<%=nPage%>&bma=<%=Rec("ID")%>&bComm=<%="detail"%>">&nbsp;<%=Rec("Kyhieu")%></A></td>
		<td width="75%" style="FONT-STYLE: italic"><A onmouseout="window.status='';return true" onmouseover="window.status='Ch&#7885;n th&#244;ng tin';return true" style='text-decoration: none;color:blue;font-weight:<%=fontweight%>'  href="frmDonVi.asp?page=<%=nPage%>&bma=<%=Rec("ID")%>&bComm=<%="detail"%>"><% =bTab%><%=Rec("tendonvi")%></A></td>	
	<% else %>	
	    <td width="10%" align="center"><A onmouseout="window.status='';return true" onmouseover="window.status='Ch&#7885;n th&#244;ng tin';return true" style='text-decoration: none;color:blue;font-weight:<%=fontweight%>' href="frmDonVi.asp?page=<%=nPage%>&bma=<%=Rec("ID")%>&bComm=<%="detail"%>">&nbsp;<%=Rec("ID")%></A></td>
		<td width="15%"><A onmouseout="window.status='';return true" onmouseover="window.status='Ch&#7885;n th&#244;ng tin';return true" style='text-decoration: none;color:blue;font-weight:<%=fontweight%>' href="frmDonVi.asp?page=<%=nPage%>&bma=<%=Rec("ID")%>&bComm=<%="detail"%>">&nbsp;<%=Rec("Kyhieu")%></A></td>
	    <td width="75%"><A onmouseout="window.status='';return true" onmouseover="window.status='Ch&#7885;n th&#244;ng tin';return true" style='text-decoration: none;color:blue;font-weight:<%=fontweight%>'  href="frmDonVi.asp?page=<%=nPage%>&bma=<%=Rec("ID")%>&bComm=<%="detail"%>"><% =bTab%><%=Rec("Tendonvi")%></A></td>
	<% end if %>
  </tr>
<%			STT=STT+1
			Rec.MoveNext
			cnt=cnt+1
			howmanyrecs=howmanyrecs+1
		loop
	end if
	rec.Close
	set rec=nothing
	
%>  
</table>
 <!-- Phan trang -->
<br>
<table width="98%" cellspacing=0 cellpadding=0>
<tr>
<% if nPage > 0  and nPageCount >0 then %>  
	<td width="20%"><b>Trang&nbsp; <%=nPage%>/<%=nPageCount%></b></td>
<!-- 	<td width= "80%" align="right">
	<% if nPageCount > 1 then %>    
		Chuyển nhanh tới trang:&nbsp;
		<select name="page" style="width:45;height:23;font-size:8pt" size=1 onchange="javaScript:ChangePage()">
			<% for i = 1 to nPageCount%>	
				<% if bComm<>"search"  then
					strURL="frmDonVi.asp?bsearch="& bsearch &"&Loaitim="& loaitim &"&page="+ cstr(i) + "&bComm="
				else
					strURL="frmDonVi.asp?bsearch="& bsearch &"&Loaitim="& loaitim &"&page="+ cstr(i) + "&bComm=search"
				end if %>
				<option value="<%=strURL%>" <%=chkSelected(i, nPage)%>><%=i%></option>
			<% next %>    
		</select>
		</td>                                  
	<% end if %> -->
<% end if %>
<td width= "80%" align="center"><%=Paging(nPage, nPageCount, nRecCount)%></td>
  </tr>
</table>
</body>
</form>
</html>
<%
	Conn.Close
	Set Conn=nothing
	set ClsFun=nothing
%>